<template>
  <div class="prescription-management">
    <!-- 表单 -->
    <el-form :model="newPrescription" ref="formRef" label-width="100px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="newPrescription.name"></el-input>
      </el-form-item>
      <el-form-item label="配方" prop="formula">
        <el-input type="textarea" v-model="newPrescription.formula"></el-input>
      </el-form-item>
      <el-form-item label="功效" prop="efficacy">
        <el-input v-model="newPrescription.efficacy"></el-input>
      </el-form-item>
      <el-form-item label="制法" prop="preparation">
        <el-input v-model="newPrescription.preparation"></el-input>
      </el-form-item>
      <el-form-item label="验证" prop="validation">
        <el-input v-model="newPrescription.validation"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addPrescription">新增验方</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table :data="prescriptions" style="width: 100%">
      <el-table-column prop="id" label="ID" width="180"></el-table-column>
      <el-table-column prop="name" label="名称" width="180"></el-table-column>
      <el-table-column prop="formula" label="配方"></el-table-column>
      <el-table-column prop="efficacy" label="功效"></el-table-column>
      <el-table-column prop="preparation" label="制法"></el-table-column>
      <el-table-column prop="validation" label="验证"></el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="mini" @click="editPrescription(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deletePrescription(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑对话框 -->
    <el-dialog title="编辑验方" :visible.sync="dialogVisible" width="50%">
      <el-form :model="editPrescriptionForm" ref="editFormRef" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="editPrescriptionForm.name"></el-input>
        </el-form-item>
        <el-form-item label="配方" prop="formula">
          <el-input type="textarea" v-model="editPrescriptionForm.formula"></el-input>
        </el-form-item>
        <el-form-item label="功效" prop="efficacy">
          <el-input v-model="editPrescriptionForm.efficacy"></el-input>
        </el-form-item>
        <el-form-item label="制法" prop="preparation">
          <el-input v-model="editPrescriptionForm.preparation"></el-input>
        </el-form-item>
        <el-form-item label="验证" prop="validation">
          <el-input v-model="editPrescriptionForm.validation"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmEditPrescription">确认修改</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newPrescription: {
        name: '',
        formula: '',
        efficacy: '',
        preparation: '',
        validation: ''
      },
      prescriptions: [],
      dialogVisible: false,
      editPrescriptionForm: {}
    };
  },
  created() {
    this.loadPrescriptions();
  },
  methods: {
    async loadPrescriptions() {
      try {
        const res = await this.$axios.get('/api/prescriptions');
        this.prescriptions = res.data;
      } catch (error) {
        console.error('获取验方列表失败:', error);
      }
    },
    async addPrescription() {
      try {
        await this.$axios.post('/api/prescriptions', this.newPrescription);
        this.loadPrescriptions();
        this.resetForm();
      } catch (error) {
        console.error('新增验方失败:', error);
      }
    },
    resetForm() {
      this.newPrescription = { name: '', formula: '', efficacy: '', preparation: '', validation: '' };
      this.$refs.formRef.resetFields();
    },
    editPrescription(prescription) {
      this.editPrescriptionForm = { ...prescription };
      this.dialogVisible = true;
    },
    async confirmEditPrescription() {
      try {
        await this.$axios.put(`/api/prescriptions/${this.editPrescriptionForm.id}`, this.editPrescriptionForm);
        this.dialogVisible = false;
        this.loadPrescriptions();
      } catch (error) {
        console.error('更新验方失败:', error);
      }
    },
    async deletePrescription(id) {
      try {
        await this.$axios.delete(`/api/prescriptions/${id}`);
        this.loadPrescriptions();
      } catch (error) {
        console.error('删除验方失败:', error);
      }
    }
  }
};
</script>

<style scoped>
.prescription-management {
  padding: 20px;
}
</style>